package com.rz.paas.test.SettingDetail

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.orhanobut.hawk.Hawk
import com.rz.paas.test.R
import com.rz.paas.test.base.MiddleActivity
import com.rz.paas.test.constant.AppConstant
import com.rz.paas.test.constant.OptionsData
import com.rz.paas.test.constant.toast
import kotlinx.android.synthetic.main.activity_setting_detail.*
import kotlinx.android.synthetic.main.custom_action_bar.*

class SettingDetailActivity : MiddleActivity() {

    companion object {
        const val OPTION_ID = "OPTION_ID"
        const val TYPE_ITEM_SELECT = "TYPE_ITEM_SELECT"
        const val SUB_TITLE_STRING = "SUB_TITLE_STRING"
        fun open(context: Activity, optionId: Int, select: Int, subTitle: String = "") {
            val intent = Intent(context, SettingDetailActivity::class.java)
            intent.putExtra(OPTION_ID, optionId)
            intent.putExtra(TYPE_ITEM_SELECT, select)
            intent.putExtra(SUB_TITLE_STRING, subTitle)
//            context.startActivity(intent)
            context.startActivityForResult(intent, 2000)
        }
    }

    private val adapter by lazy {
        SettingDetailAdapter(baseContext)
    }
    val optionId by lazy {
        intent.getIntExtra(OPTION_ID, 0)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_setting_detail)

        val subTitle = intent.getStringExtra(SUB_TITLE_STRING)
        if (TextUtils.isEmpty(subTitle)) {
            sub_title.text = "设置"
        } else {
            sub_title.text = subTitle
        }


        val selectNum = intent.getIntExtra(TYPE_ITEM_SELECT, 0)
        val optionsData = AppConstant.dataMap[optionId]
        main_title.text = optionsData.subpage
        type_title.text = optionsData.title

        initCustomView(optionId, optionsData)

        initRecyclerView(optionsData, selectNum)

        itemSelect(optionsData)

        custom_push_switch.setOnCheckedChangeListener { buttonView, isChecked ->
            if (isChecked) {
                notifyItemChanged(2)
                Hawk.put(optionsData.name, 2)
            } else {
                custom_env_url.setText("")
                notifyItemChanged(selectedIndex)
                Hawk.put(optionsData.name, selectedIndex)
            }
        }
    }

    private fun initCustomView(optionId: Int, optionsData: OptionsData) {
        if (optionId == AppConstant.ENV_DATA_INDEX) {
            custom_push_content.visibility = View.VISIBLE
        } else {
            custom_push_content.visibility = View.GONE
        }
        if (go_back_iv != null) {
            go_back_iv.setOnClickListener {
                if (custom_push_switch.isChecked) {
                    if (TextUtils.isEmpty(custom_env_url.text.toString())) {
                        toast("请填写自定义环境")
                        return@setOnClickListener
                    } else {
                        Hawk.put(AppConstant.CUSTOM_ENV, custom_env_url.text.toString())
                    }

                }
                finish()
            }
        }


    }

    private fun initRecyclerView(
        optionsData: OptionsData,
        selectNum: Int
    ) {
        item_detail.layoutManager = LinearLayoutManager(baseContext)
        item_detail.adapter = adapter
        if (optionsData.resId == R.array.encode_resolution_data) {
            item_detail.addItemDecoration(StickHeaderDirection(this, true))
        }

        val stringArray = resources.getStringArray(optionsData.resId)
        val listData = mutableListOf<TypeData>()
        initData(stringArray, selectNum, listData)
        adapter.data = listData
    }

    private fun itemSelect(optionsData: OptionsData) {
        adapter.setItemClickListener { data, position ->
            notifyItemChanged(position)

            Hawk.put(optionsData.name, position)
            intent.putExtra(OPTION_ID, optionId)
            setResult(RESULT_OK, intent)
            finish()
        }
    }

    private fun notifyItemChanged(position: Int) {
        if (selectedIndex >= adapter.data.size) return
        val oldSelected = adapter.data[selectedIndex]
        oldSelected.isSelected = false
        adapter.setData(selectedIndex, oldSelected)

        if (position < adapter.data.size) {
            val newSelected = adapter.data[position]
            newSelected.isSelected = true
            selectedIndex = position
            adapter.setData(position, newSelected)
        }


    }

    var selectedIndex = 0
    private fun initData(
        stringArray: Array<out String>, selectNum: Int, listData: MutableList<TypeData>
    ) {
        for (index in stringArray.indices) {
            val typeData = TypeData()
            typeData.name = stringArray[index]
            if (typeData.name.contains("*")) {
                typeData.groupName = typeData.name.split("*")[1]
                if (typeData.groupName.toInt() >= 720) {
                    typeData.groupName = "其他"
                }
            }

            if (selectNum == index) {
                typeData.isSelected = true
                selectedIndex = index
            } else {
                typeData.isSelected = false
            }

            listData.add(typeData)
        }
        if (optionId == AppConstant.ENV_DATA_INDEX && selectNum == 2) {
            custom_push_switch.isChecked = true
            custom_env_url.setText(Hawk.get(AppConstant.CUSTOM_ENV, ""))
        }
    }
}